package com.xiaotu.spider.license.pageprocessor;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.TVChangeModel;
import com.xiaotu.common.util.CacheHandler;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.HttpUtils;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

/**
 * @类名 TVChangePageProcessor
 * @日期 2017年10月09日
 * @作者 王艳龙
 * @功能 电视剧备案变更爬取
 */
@Component("TVChangePageProcessor")
public class TVChangePageProcessor extends AUrlPageProcessor
{
	private String start_url = "https://www.baidu.com/";
	
    @Autowired
    private CacheHandler cacheHandler;

    @SuppressWarnings("unchecked")
    @Override
    public void process(Page page)
    {
    	this.pageProcess(page);
    }

    private void pageProcess(Page page)
    {
    	try {
			Connection conn = HttpUtils.connent("https://dsj.nrta.gov.cn//tims/site/views/applications.shanty?appName=changing");
			conn.get();
			Response rs = conn.response();
			String html = rs.body();
			if(StringUtil.isNotEmpty(html)){
				Document documentHtml = Jsoup.parse(html,"utf8");
				Elements linkList = documentHtml.select("table[align=center]").select("tbody tr td").select("a[target=_blank]");
				Set<String> linkSet = new HashSet<String>();
				for(Element linkElementList : linkList){
					String linkUrl = linkElementList.select("a").attr("href");
					if(StringUtils.isNotBlank(linkUrl)){
						linkUrl = linkUrl.replace("/tims/site/", "https://dsj.nrta.gov.cn/tims/site/");
					}
					if(!cacheHandler.hExists(Constants.DataRedisKey.Change_History.toString(),linkUrl)){
						linkSet.add(linkUrl);
					}
				}
					if(!linkSet.isEmpty()){
						this.tvProcess(page,linkSet);
					}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

    /**
     * 获取每个月份的公示信息
     * 
     * @param page
     */
    private void tvProcess(Page page,Set<String> linkSet)
    {
		try {
			Set<TVChangeModel> tvSets = new HashSet<TVChangeModel>();
			for (String linkUrl : linkSet) {
				Connection conn = HttpUtils.connent(linkUrl);
				conn.get();
				Response rs = conn.response();
				String html = rs.body();
				if(StringUtil.isNotEmpty(html)){
					Document documentHtml = Jsoup.parse(html,"utf8");
					Html divHtml = new Html(documentHtml.toString());
					// 网页表格对象
					List<Selectable> tableList = divHtml.xpath("//td[@valign='top']/table/tbody/tr[2]/td/table").nodes();
					if (tableList == null || tableList.size() < 1)
						throw new SpiderException(SpiderExceptionCode.DataInvalideException);
					for (Selectable table: tableList) {
						// 剧名表更
						if (table.toString().contains("原剧名")) {
							List<Selectable> trList = table.xpath("//tr").nodes();
							int begin = 0;
							for (Selectable tr : trList) {
								Set<TVChangeModel> tvSet1 = new HashSet<>();
								// 去除表头
								if (begin == 0) {
									begin = 1;
									continue;
								}
								String oldtvname =tr.xpath("//td[1]/text()").toString();
								String tvname = tr.xpath("//td[2]/text()").toString();
								String producecompany =tr.xpath("//td[3]/text()").toString();
								tvSet1.add(new TVChangeModel(Constants.TargetType.OTHER_TYPE_ID,oldtvname, tvname, producecompany,"","","","",1));
								tvSets.addAll(tvSet1);
							}
							
							
						}
						//单位变更
						else if (table.toString().contains("原制作单位")) {
							List<Selectable> trList = table.xpath("//tr").nodes();
							int begin = 0;
							for (Selectable tr : trList) {
								Set<TVChangeModel> tvSet2 = new HashSet<>();
								// 去除表头
								if (begin == 0) {
									begin = 1;
									continue;
								}
								String tvname = tr.xpath("//td[1]/text()").toString();
								String oldproducecompany = tr.xpath("//td[2]/text()").toString();
								String producecompany = tr.xpath("//td[3]/text()").toString();
								tvSet2.add(new TVChangeModel(Constants.TargetType.OTHER_TYPE_ID,"",tvname, producecompany,oldproducecompany,"","","",2));
								tvSets.addAll(tvSet2);
							}
						}
						else if (table.toString().contains("增减数量")) {
							List<Selectable> trList = table.xpath("//tr").nodes();
							int begin = 0;
							for (Selectable tr : trList) {
								Set<TVChangeModel> tvSet3 = new HashSet<>();
								// 去除表头
								if (begin == 0) {
									begin = 1;
									continue;
								}
								String tvname = tr.xpath("//td[1]/text()").toString();
								String setschange = tr.xpath("//td[2]/text()").toString();
								String oldsets = getOldSets(setschange);
								String sets = getSets(setschange);
								String producecompany = tr.xpath("//td[3]/text()").toString();
								tvSet3.add(new TVChangeModel(Constants.TargetType.OTHER_TYPE_ID,"",tvname, producecompany,"",sets,oldsets,"",3));
								tvSets.addAll(tvSet3);
							}
						}
					}
				}
		}
				if (!tvSets.isEmpty()){
					page.putField(TVChangeModel.class.getName(), tvSets);
					page.putField(URL_KEY,linkSet);
				}
						
		} catch (IOException e) {
			e.printStackTrace();
		}
		
    }
		
	// 获取变更字符串中的原始集数
	private String getOldSets(String s) {
		String sets = "";
		for (int i = 0; i < s.length(); i++) {
			String si = s.substring(i, i + 1);
			if (si.matches("\\d")) {
				sets += si;
			}

			if (i > 3) {
				break;
			}
		}
		return sets;
	}

	// 获取变更字符串中的新集数
	private String getSets(String s) {
		String sets = "";
		int j = 0;
		for (int i = 0; i < s.length(); i++) {
			if (i < 3) {
				continue;
			}

			String si = s.substring(i, i + 1);
			if (si.matches("\\d")) {
				sets += si;
				j++;
			}

			if (j > 3) {
				break;
			}

		}
		return sets;
	}
	
	@Override
    protected String[] getStartRequestUrls()
    {
        //return new String[]{"http://dsj.nrta.gov.cn//tims/site/views/applications.shanty?appName=changing"};
		return new String[]{start_url};
    }

    @Override
    protected String getSpiderType()
    {
        return Constants.SpiderTaskType.TV_CHANGE;
    }

    @Override
    public String getPageDataType(Page page)
    {
    	return Constants.PageDataType.SEARCH_LIST;
    	  
    }	

}
